<?php
namespace app\shell;

use app\service\AppInfoTableService;
use app\service\SendTemplateRelationService;
use app\service\SendTemplateService;
use share\console\CConsole;
use share\console\CLIRunner;
use share\model\SceneTableModel;
use share\service\AliyunDySmsService;
use share\service\SceneTableService;
use share\service\SendCodeService;

/**
 * 短信发送模版场景匹配
 */
class CSendTemplate extends CLIRunner{
    
    /**
     * 从 sendsecret_db 同步数据
     */
    public function async_from_sendsecret_action(){

    }

    public function sms_template_test_action(){
    //   $send = AliyunDySmsService::sendSMS('12099791081',['code' => '123456'],[
    //     "signName" => "智云智联",
    //     "TemplateCode" => "SMS_276825175",
    //     "accessKeyId" => "LTAI5tKJxvnAawd34MhmsrRT",
    //     "accessKeySecret" => "hLZfRqYdPyBYrIZUaAflOsOY8kOPEj",
    //   ]);

    // $send = AliyunDySmsService::sendSMS('19143089421',['code' => '123456'],[
    //     "signName" => "ZHIYUN",
    //     "TemplateCode" => "SMS_460675053",
    //     "accessKeyId" => "LTAI5tKJxvnAawd34MhmsrRT",
    //     "accessKeySecret" => "hLZfRqYdPyBYrIZUaAflOsOY8kOPEj",
    //   ]);
    //   return $send;

    }

    /**
     * 对比发送配置
     */
    public function test_send_config_action(){
        $res = [];
        $sendType = [SendTemplateService::SEND_TYPE_OF_SMS,SendTemplateService::SEND_TYPE_OF_EMAIL];
        $language = [SendTemplateService::SEND_LANGUAGE_OF_CN,SendTemplateService::SEND_LANGUAGE_OF_EN];

        $appInfo = AppInfoTableService::getAllByFilter([]);
        $err = [];
        foreach($sendType as $type){
            foreach($appInfo as $app){
                foreach($language as $lang){
                    $relation = SendTemplateRelationService::getBestMatchRelation($app['vendor'],$app['package'],$lang,$type,SendCodeService::SEND_CODE_SCENE_LOGIN);
                    if(empty($relation)){
                        $err[] = [
                            'vendor' => $app['vendor'],
                            'package' => $app['package'],
                            'lang' => $lang == SendTemplateService::SEND_LANGUAGE_OF_CN ? 'CN' : 'EN',
                            'type' => $type == SendTemplateService::SEND_TYPE_OF_SMS ? 'SMS' : 'EMAIL',
                        ];
                    }
                    else{
                        $res[] = [
                            'vendor' => $app['vendor'],
                            'package' => $app['package'],
                            'lang' => $lang == SendTemplateService::SEND_LANGUAGE_OF_CN ? 'CN' : 'EN',
                            'type' => $type == SendTemplateService::SEND_TYPE_OF_SMS ? 'SMS' : 'EMAIL',
                            'no' => $relation['no'],
                            'appid' => $relation['appid'],

                            'sign' => $relation['title'],
                            'config_name' => $relation['config_name'],
                            'name' => $relation['name'],
                            
                            // 'sign' => iconv("UTF-8", "GB2312//IGNORE", $relation['title']),                          
                            // 'config_name' => iconv("UTF-8", "GB2312//IGNORE",  $relation['config_name']),
                            // 'name' =>  iconv("UTF-8", "GB2312//IGNORE", $relation['name']),
                        ];
                    }
                }
            }
        }

        $fp = fopen(date("YmdHis").'.csv', 'w');

        $keys = array_keys($res[0]);
        fputcsv($fp, $keys);
        foreach ($res as $item) {
            $values = array_values($item);
            fputcsv($fp, $values);
        }

        fclose($fp);

        return ['succ' => $res,'error' => $err];
 
    }
}